<template>
  <div>
    <h2>产品申请</h2>
    <el-form
      ref="productApp"
      status-icon
      :model="productAppForm"
      label-width="40%"
    >
      <el-form-item label="产品名称" prop="productappName">
        <el-input
          v-model="productApp.productappName"
          style="width: 280px"
        ></el-input>
      </el-form-item>
      <el-form-item label="产品简介" prop="productappDesc">
        <el-input
          v-model="productApp.productappDesc"
          style="width: 280px"
        ></el-input>
      </el-form-item>
      <el-form-item label="启动资金" prop="productappRegisteredCapital">
        <el-input
          v-model="productApp.productappRegisteredCapital"
          style="width: 280px"
        ></el-input>
      </el-form-item>
      <el-form-item label="放贷额度" prop="productappPicture">
        <el-input
          v-model="productApp.productappPicture"
          style="width: 280px"
        ></el-input>
      </el-form-item>

      <el-form-item label="产品类型">
        <el-select
          v-model="productApp.productTypeId"
          placeholder="请选择产品类型"
        >
          <el-option
            :label="item.productTypeName"
            :value="item.productTypeId"
            v-for="(item, index) in productType"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="还款方式">
        <el-select
          v-model="productApp.productappBy2"
          placeholder="请选择还款方式"
        >
          <el-option
            :label="item.averageName"
            :value="item.averageId"
            v-for="(item, index) in average"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="公司名称">
        <el-select
          v-model="productApp.companyId"
          @change="chooseCompany(productApp.companyId)"
          placeholder="请选择公司名称"
        >
          <el-option
            :label="change.companyName"
            :value="change.changeRoleId"
            v-for="(change, index) in changeRole"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属银行">
        <el-select v-model="productApp.bankUserId" placeholder="请选择银行">
          <el-option
            :label="item.bankName"
            :value="item.bankId"
            v-for="(item, index) in allBank"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="公司账户">
        <el-input
          v-model="productApp.productappBy1"
          style="width: 280px"
        ></el-input>
      </el-form-item>

      <el-form-item label="相关文件">
        <el-upload
          name="file"
          :headers="upload.headerObj"
          :on-success="handleSuccess"
          :on-error="handleError"
          class="upload-demo"
          drag
          action="http://localhost:8888/uploadLJ"
          multiple
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">
            文件大小不超过30mb，多文件上传请压缩
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="application()"
        >提 交 申 请</el-button
      >
      <el-button @click="clean()">清 空</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // var productappName = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("产品名称不能为空"));
    //   } else {
    //     if (this.productApp.productappName !== '') {
    //       this.$refs.productApp.validateField('productappName');
    //     }
    //     callback();
    //   }
    // };
    // var productappDesc = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("产品描述不能为空"));
    //   } else {
    //     if (this.productApp.productappDesc !== '') {
    //       this.$refs.productApp.validateField('productappDesc');
    //     }
    //     callback();
    //   }
    // };
    // var productappRegisteredCapital = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("请输入产品启动资金，单位为元"));
    //   } else {
    //     if (this.productApp.productappRegisteredCapital !== '') {
    //       this.$refs.productApp.validateField('productappRegisteredCapital');
    //     }
    //     callback();
    //   }
    // };
    // var productappPicture = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("请输入产品贷款额度，单位为元"));
    //   } else {
    //     if (this.productApp.productappPicture !== '') {
    //       this.$refs.productApp.validateField('productappPicture');
    //     }
    //     callback();
    //   }
    // };

    return {
      upload: {
        headerObj: {
          //Spring security时需要传递token，否则不需要该配置
          Authorization: sessionStorage.getItem("authorization"),
        },
      },

      productType: [],
      changeRole: [],
      allBank: [],
      average: [],
      productApp: {
        // productappName: '',
        // productappDesc: '',
        // productappRegisteredCapital: '',
        // productappPicture: '',
      },
      // rules: {
      //   productappName: [
      //     { validator:productappName, trigger: "blur" },
      //   ],
      //   productappDesc: [
      //     { validator:productappDesc, trigger: "blur" },
      //   ],
      //   productappRegisteredCapital: [
      //     { validator:productappRegisteredCapital, trigger: "blur" },
      //   ],
      //   productappPicture: [
      //     { validator:productappPicture, trigger: "blur" },
      //   ]
      // },
      productAppForm: true,
    };
  },
  methods: {
    handleError() {
      if (res.code !== 200) {
        this.$message.error("上传失败");
      }
    },
    handleSuccess(res, file) {
      if (res.code == 200) {
        this.$message.success("上传成功");
      }
    },

    // 进入页面查询产品类型
    selectProductType() {
      this.$axios.get("/productTypes").then((res) => {
        this.productType = res.data.data.productType;
      });
    },

    // 进入页面查询所有公司详情
    selectChangeRole() {
      this.$axios.get("/changeRoles/selectAllConpany").then((res) => {
        this.changeRole = res.data.data.changeRoles;
      });
    },

    // 进入页面查询所有银行
    selectAllBank() {
      this.$axios.get("/banks/selectAllBank").then((res) => {
        this.allBank = res.data.data.allBank;
      });
    },

    // 进入页面查询所有还款方式
    selectAllRepayment() {
      this.$axios.get("/averages").then((res) => {
        this.average = res.data.data.average;
      });
    },

    // 提交产品申请表，插入数据
    application() {
      // this.$refs[productApp].validate((valid) => {
      //   if (valid) {
      //     alert("submit!");
      //   } else {
      //     console.log("error submit!!");
      //     return false;
      //   }
      // });

      this.$axios.post("/productApps", this.productApp).then((res) => {
        if (res.data.code == 200) {
          this.$message.success("提交申请成功");
          this.productApp = {};
        } else {
          this.$message.warn("申请失败，请稍后再试");
        }
      });
    },

    // 清空当前表单
    clean() {
      this.productApp = {};
    },

    handleSuccess(res, file) {
      if (res.code == 200) {
        this.imageUrl = res.message;
        this.productApp.headImg = res.message;
      }
    },
    beforeUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    chooseCompany(id) {
      this.$axios.post("/changeRoles/" + id).then((res) => {
        this.productApp = res.data.data.productApp;
      });
    },
  },
  mounted() {
    this.selectProductType();
    this.selectChangeRole();
    this.selectAllBank();
    this.selectAllRepayment();
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>